//
//  YJDataModel.m
//  Product
//
//  Created by 吴志勇 on 2020/4/2.
//  Copyright © 2020 wzy. All rights reserved.
//

#import "YJDataViewModel.h"
#import "LoginApi.h"
#import "YJModel.h"
#import "TableViewCell.h"
#import "MyMB.h"

@interface YJDataViewModel()

/*  */
@property (nonatomic, strong) NSMutableArray<YJModel *> *dataArray; //使用泛型
@property (nonatomic, copy)NSString *currentPage;

/*  */
@property (nonatomic, strong) NSMutableDictionary *cellHDict;

@end


@implementation YJDataViewModel



// 请求数据
- (void)requestData:(void(^)(void))sBlock fBlock:(void(^)(void))fBlock isRefresh:(BOOL)isRefresh{
    if (isRefresh) {
        [self.dataArray removeAllObjects];
        self.currentPage = @"0";
        
    }
    
    if (!self.currentPage) {
        self.currentPage = @"0";
    }
    
    NSMutableDictionary *argument = [NSMutableDictionary dictionary];
    [argument setValue:self.currentPage forKey:@"page"];
    [argument setValue:@"10" forKey:@"limit"];
    [argument setValue:@"15715655233" forKey:@"phone"];
    [argument setValue:@"15715655233" forKey:@"Myphone"];
    LoginApi *api = [[LoginApi alloc] initWithUrl:@"/dataApi/appointment/getAppointment" requestArgument:argument];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKRequest * _Nullable request, NSDictionary * _Nullable result, BOOL success) {
        NSDictionary *dict = result[@"data"];
        self.currentPage = [NSString stringWithFormat:@"%ld",self.currentPage.integerValue + 1];
        NSArray *array = dict[@"records"];
        for (NSDictionary *dict in array) {
            YJModel *model = [YJModel modelWithArray:dict];
            [self.dataArray addObject:model];
        }
        if (sBlock) {
            sBlock();
        }
        
   
    } failure:^(__kindof YTKRequest * _Nullable request, NSString * _Nullable errorInfo) {
        if (fBlock) {
            fBlock();
        }
    }];
}


- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"yjCellId"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.row];
    cell.dzClick = ^{
        [[MyMB sharedInstance] showText:@"点赞功能"];
    };
    
    cell.plClick = ^{
        [[MyMB sharedInstance] showText:@"品论功能"];
    };
    
    cell.joinClick = ^{
       [[MyMB sharedInstance] showText:@"加入功能"];
    };
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (self.click) {
//        NSString *str = [NSString stringWithFormat:@"你点击了第%ldcell",indexPath.row];
//
//        self.click(str);
//    }
}


- (NSMutableDictionary *)cellHDict {
    if (!_cellHDict) {
        _cellHDict = [NSMutableDictionary dictionary];
    }
    return  _cellHDict;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    YJModel *model = self.dataArray[indexPath.row];
    

   
    
    return self.dataArray[indexPath.row].cellHeight;
}

@end
